package com.fsi.server;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;

import com.fsi.client.ApplicationService;
import com.fsi.client.Customer;
import com.fsi.client.FSIException;
import com.fsi.client.LoginInfo;
import com.fsi.client.Shuttle;
import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

/**
 * The server side implementation of the RPC service.
 */
@SuppressWarnings("serial")
public class ApplicationServiceImpl extends RemoteServiceServlet implements
		ApplicationService {
	private static final Logger log = Logger.getLogger(ApplicationService.class
			.getName());
	private static final long serialVersionUID = 1L;
	private final CustomerDAO customerDAO = new CustomerJdoDAO();
	private final PhotoDAO photoDAO = new PhotoDAO();

	@Override
	public LoginInfo login(String requestUri) {
		UserService userService = UserServiceFactory.getUserService();
		User user = userService.getCurrentUser();

		LoginInfo loginInfo = new LoginInfo();

		if (user != null) {
			loginInfo.setLoggedIn(true);
			loginInfo.setEmailAddress(user.getEmail());
			loginInfo.setNickname(user.getNickname());
			loginInfo.setLogoutUrl(userService.createLogoutURL(requestUri));
		} else {
			loginInfo.setLoggedIn(false);
			loginInfo.setLoginUrl(userService.createLoginURL(requestUri));
		}

		return loginInfo;
	}

	@Override
	public List<Customer> listCustomers() throws FSIException {
		checkLoggedIn();
		List<Customer> listCustomers = customerDAO.listCustomers();
		return new ArrayList<Customer>(listCustomers);
	}

	@Override
	public void addNewCustomer(Customer customer) throws FSIException {
		checkLoggedIn();
		customerDAO.addCustomer(customer);
	}

	@Override
	public void deleteCustomer(Customer customer) throws FSIException {
		checkLoggedIn();
		customerDAO.deleteCustomer(customer);
	}

	public void checkLoggedIn() throws FSIException {
		if (getUser() == null) {
			throw new FSIException("Not Logged In");
		}
	}

	private User getUser() {
		UserService userService = UserServiceFactory.getUserService();
		return userService.getCurrentUser();
	}

	@Override
	public void testMethod() throws FSIException {
		throw new FSIException(FSIException.FSIEXCEPTION_COULDNOTDELETECUSTOMER);
	}

	@Override
	public boolean findCustomerByName(String name) {
		return false;
	}

	@Override
	public Customer getCustomerByEmail(String emailAddress) {
		return customerDAO.getCustomerByEmail(emailAddress);
	}

	@Override
	public Customer updateCustomerDetails(Customer customer, int updateType) {
		return customerDAO.updateCustomerDetails(customer, updateType);
	}

	@Override
	public List<Shuttle> getPhotos(String customerID) {
		return photoDAO.getPhotos(customerID);
	}

	@Override
	public void deletePhoto(String customerID, String photoID) {
		photoDAO.deletePhoto(customerID, photoID);
	}

	@Override
	public void logClientMsg(String msg) {
		log.info(msg);
	}

}
